import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useLogin } from '../api/user'
import {rm} from '../utils/index'
export function Login(){
    const router = useRouter()
     const codes=('') 
      //登录数据
      const ruleForm = reactive({
        username: '',
        password: '',
        code: '',
      })
    const checkAge = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入验证码'))
        }
      
      }
      
      const validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'))
        } else {
          callback()
        }
      }
      const validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          callback()
        }
      }
      
      //效验规则
      const rules = reactive({
        username: [{ validator: validatePass, trigger: 'blur' }],
        password: [{ validator: validatePass2, trigger: 'blur' }],
        code: [{ validator: checkAge, trigger: 'blur' }],
      })
      
      const submitForm = formEl => {
        useLogin(ruleForm).then(res => {
          //  console.log(res);
          if (res.code === 1) {
            ElMessage({
              message: res.message,
              type: 'success',
            })
             router.push('/home/homes')
            localStorage.setItem('token', res.token)
            localStorage.setItem('userinfo', res.userinfo)
          } else {
            ElMessage({
          message: res.message,
          type: 'warning',
        })
          }
      
        })
      }

   

      return {
        ruleForm,
        rules,
        submitForm
      }
}